import { createRouter, createWebHistory } from 'vue-router'
import MapindexView from '@/views/CattleMap/CattleMap.vue'
import RecordindexView from '@/views/record/RecordindexView'
import RecordContentView from '@/views/record/RecordContentView'
import RanklistindexView from '@/views/ranklist/RanklistindexView'
import UserBotindexView from '@/views/user/bot/UserBotindexView'
import NotFoundView from '@/views/error/NotFoundView'
import AddFriend from "@/views/AddFriends/AddFriend.vue";
import ReceiveFriend from "@/views/ReceiveFriend/ReceiveFriend.vue";
import ProFileView from "@/views/user/profile/ProFileView.vue";
import FriendList from "@/views/FriendList/FriendList.vue";
import TemperatureIndexView from "@/views/CattleTemperatureiHistory/CattleTemperatureiHistory.vue";
import WeightIndexView from "@/views/CattleWeightHistory/CattleWeightHistory.vue";
import LocationIndexView from "@/views/CattleLocationHistory/CattleLocationHistory.vue";
import SicknessIndexView from "@/views/CattleSickness/CattleSickness.vue";
import ManagementIndexView from "@/views/CattleManagement/CattleManagement.vue";
import HomeIndexView from '@/views/CattleHome.vue'
//注册登录等

import UserAccountLoginView from '@/views/user/account/UserAccountLoginView'
import UserAccountRegisterView from '@/views/user/account/UserAccountRegisterView'
import UserAccountForgetView from '@/views/user/account/UserAccountForgetView'
import CattleFinance from '@/views/CattleFinance/CattleFinance.vue'



import store from '@/store/index'


const routes = [
  {
    path: "/",
    name: "home",
    redirect: "/home/",
    meta: {
      requestAuth: true,
    }
  },
  {
    path: "/home/",
    name: "home_index",
    component: HomeIndexView,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: "/map/",
    name: "map_index",
    component: MapindexView,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: "/temperature/",
    name: "temperature_index",
    component: TemperatureIndexView,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: "/weight/",
    name: "weight_index",
    component: WeightIndexView,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: "/location/",
    name: "location_index",
    component: LocationIndexView,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: "/sickness/",
    name: "sickness_index",
    component: SicknessIndexView,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: "/management/",
    name: "management_index",
    component: ManagementIndexView,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: '/finance',
    name: 'finance_index',
    component: CattleFinance,
    meta: {
      requiresAuth: true,
    }
  },

  {
    path: "/record/",
    name: "record_index",
    component: RecordindexView,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: "/record/",
    name: "record_content",
    component: RecordContentView,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: "/ranklist/",
    name: "ranklist_index",
    component: RanklistindexView,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: "/friendlist/",
    name: "friendlist_index",
    component: FriendList,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: "/user/bot/",
    name: "user_bot_index",
    component: UserBotindexView,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: "/user/account/login/",
    name: "user_account_login",
    component: UserAccountLoginView,
    meta: {
      requestAuth: false,
    }
  },
  {
    path: "/user/account/forget/",
    name: "user_account_forget",
    component: UserAccountForgetView,
    meta: {
      requestAuth: false,
    }
  },
  {
    path: "/add_friend/",
    name: "add_friend",
    component: AddFriend,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: "/receive_friend/",
    name: "receive_friend",
    component: ReceiveFriend,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: "/user/account/profile/",
    name: "profile",
    component: ProFileView,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: "/user/account/register",
    name: "user_account_register",
    component: UserAccountRegisterView,
    meta: {
      requestAuth: false,
    }
  },
  {
    path: "/404/",
    name: "not_found_index",
    component: NotFoundView,
    meta: {
      requestAuth: false,
    }
  },
  {
    path: "/:catchAll(.*)",
    redirect: "/404/"
  },



]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.requestAuth && !store.state.user.is_login) {
    next({ name: "user_account_login" });
  } else {
    next();
  }
})

export default router
